package Draw0513;

import java.awt.*;
import java.util.HashMap;
import java.util.Random;

public class KLine extends ShapeV2{
    Random random = new Random();
    HashMap<Slider,Point> map = new HashMap<>();

    public KLine(int x1,int y1,int x2,int y2,Color color, String type){
        this.x1 = Math.max(x1,x2);
        this.y1 = Math.min(y1,y2);
        this.x2 = Math.min(x1,x2);
        this.y2 = Math.max(y1,y2);
        this.x3 = (2*this.x1 - this.x2);
        this.y3 = this.y2;
        this.color = color;
        this.type = type;
        map.put(new Slider(this.x1,this.x2,this.y1,this.y2),new Point());
        map.put(new Slider(this.x3,this.x2,this.y3,this.y2),new Point());
        map.put(new Slider(this.x1,this.x3,this.y1,this.y3),new Point());
    }

    public void drawKLine(int x1, int y1, int x2, int y2, int x3, int y3, int r, int deepth, Graphics g){
        g.setColor(color);
        if (deepth == 0){
            g.drawLine(x1,y1,x2,y2);
            g.drawLine(x3,y3,x2,y2);
            g.drawLine(x1,y1,x3,y3);
            return;
        }
        deepth--;

        int x4=0,x5=0,x6=0,y4=0,y5=0,y6=0;
        int flag1=0,flag2=0,flag3=0;
        for (Slider s : map.keySet()){
            if (s.equals(new Slider(x1, x2, y1, y2))){
                Point p = map.get(s);
                if (p.getX() == 0){
                    x4 = (x1+x2)/2;
                    y4 = (y1+y2)/2+random.nextInt(r*2)-r;
                    map.put(s,new Point(x4,y4));
                }else {
                    x4 = (int) p.getX();
                    y4 = (int) p.getY();
                }
                flag1 = 1;
                break;
            }

        }
        if (flag1 == 0){
            x4 = (x1+x2)/2;
            y4 = (y1+y2)/2+random.nextInt(r*2)-r;
            map.put(new Slider(x1,x2,y1,y2),new Point(x4,y4));
        }

        for (Slider s : map.keySet()){
            if (s.equals(new Slider(x1, x3, y1, y3))){
                Point p = map.get(s);
                if (p.getX() == 0){
                    x5 = (x1+x3)/2;
                    y5 = (y1+y3)/2+random.nextInt(r*2)-r;
                    map.put(s,new Point(x5,y5));
                }else {
                    x5 = (int) p.getX();
                    y5 = (int) p.getY();
                }
                flag2 = 1;
                break;
            }

        }
        if (flag2 == 0){
            x5 = (x1+x3)/2;
            y5 = (y1+y3)/2+random.nextInt(r*2)-r;
            map.put(new Slider(x1,x3,y1,y3),new Point(x5,y5));
        }

        for (Slider s : map.keySet()){
            if (s.equals(new Slider(x3, x2, y3, y2))){
                Point p = map.get(s);
                if (p.getX() == 0){
                    x6 = (x3+x2)/2;
                    y6 = (y3+y2)/2+random.nextInt(r*2)-r;
                    map.put(s,new Point(x6,y6));
                }else {
                    x6 = (int) p.getX();
                    y6 = (int) p.getY();
                }
                flag3 = 1;
                break;
            }

        }
        if (flag3 == 0){
            x6 = (x3+x2)/2;
            y6 = (y3+y2)/2+random.nextInt(r*2)-r;
            map.put(new Slider(x3,x2,y3,y2),new Point(x6,y6));
        }

        r *= 0.5;
        drawKLine(x1,y1,x4,y4,x5,y5,r,deepth,g);
        drawKLine(x2,y2,x4,y4,x6,y6,r,deepth,g);
        drawKLine(x3,y3,x5,y5,x6,y6,r,deepth,g);
        drawKLine(x4,y4,x5,y5,x6,y6,r,deepth,g);


    }

    @Override
    public void drawShapeV2(Graphics g){
        drawKLine(x1,y1,x2,y2,x3,y3,100,7,g);
    }

    @Override
    public int hashCode() {
        int hash = x1 + x2 + y1 + y2;
        return hash;
    }


}
